import { useRouter } from "@tarojs/taro";
import { useCallback, useEffect, useRef } from "react";

export const useThrottle = (fn, delay, dep = []) => {
  const { current } = useRef<any>({ fn, timer: null });
  useEffect(
    function () {
      current.fn = fn;
    },
    [fn]
  );

  return useCallback(function f(...args) {
    if (!current.timer) {
      current.timer = setTimeout(() => {
        delete current.timer;
      }, delay);
      current.fn.call(this, ...args);
    }
  }, dep);
};

export const useScene = (): [boolean, string | number | undefined] => {
  const { params } = useRouter();
  const scene = params.scene;
  const exist = scene !== undefined && scene !== "undefined";
  return [exist, scene]
}